﻿using SuperWebSocket;
using System;
using System.Net;

namespace SuperWebSocketClientTest
{
    class Program
    {
        static void Main(string[] args)
        {
            SuperWebSocketClient client = new SuperWebSocketClient();           

            client.OnError += new WebSocketEventHandler(OnWebSocketError);
            client.OnLogined += new WebSocketEventHandler(OnWebSocketLogined);
            client.OnData += new WebSocketEventHandler(OnWebSocketData);
            client.OnMessage += new WebSocketEventHandler(OnWebSocketMessage);
            client.OnClosed += new WebSocketEventHandler(OnWebSocketClosed);

            client.Connect("192.168.1.156", "2345","wxdong","213344","chart","我想聊天");
            //client.Connect("192.168.1.229", "2345", "wxdong", "213344", "chart", "23444");

            System.Threading.Thread.Sleep(200);

            //client.Close();             

            Console.ReadKey();

        }

        static bool OnWebSocketError(object sender, WebSocketEventArgs e)
        {
            Console.WriteLine(e.Message);
            return false;
        }

        static bool OnWebSocketLogined(object sender,WebSocketEventArgs e)
        {           
            return false;
        }

        static bool OnWebSocketMessage(object sender,WebSocketEventArgs e)
        {
            Console.WriteLine("server say: " + ((ContextData)e.Data).Data);
            return false;
        }

        static bool OnWebSocketData(object sender, WebSocketEventArgs e)
        {
            var data = (ContextData)e.Data;
            string message = System.Text.Encoding.UTF8.GetString((byte[])data.Data);
            Console.WriteLine("server say: " + message);
            return false;
        }

        static bool OnWebSocketClosed(object sender, WebSocketEventArgs e)
        {
            Console.WriteLine("Socket 被关闭");
            return false;
        }

    }
}
